package com.ctshk.rpc.custom.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.rpc.custom.dto.trip.TripScheduleGuideDTO;
import com.ctshk.rpc.custom.entity.CustomizedTripSchedule;
import com.ctshk.rpc.custom.req.trip.TripScheduleCustomizedCenterListReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 团期 Mapper 接口
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-01-20
 */
public interface CustomizedTripScheduleMapper extends BaseMapper<CustomizedTripSchedule> {

    /**
     * 团期列表(预订中心)
     * @param req
     * @param iPage
     * @return
     */
    @Select("<script>"+"SELECT a.*  \n" +
            "FROM t_customized_trip_schedule a\n" +
            "LEFT JOIN t_customized_trip_schedule_channel b ON a.id = b.schedule_id\n" +
            "WHERE a.is_deleted = 0 AND b.visable_type = 2 AND (a.group_status = 1 OR a.group_status = 2) \n"+
            "<if test=\"req.tripNumber != null and req.tripNumber != ''\">AND a.trip_number LIKE CONCAT('%',#{req.tripNumber},'%')</if> \n" +
            "<if test=\"req.inquiryNumber != null and req.inquiryNumber != ''\">AND a.inquiry_number LIKE CONCAT('%',#{req.inquiryNumber},'%')</if> \n" +
            "<if test=\"req.quotationNumber != null and req.quotationNumber != ''\">AND a.quotation_number LIKE CONCAT('%',#{req.quotationNumber},'%')</if> \n" +
            "<if test=\"req.departurePlaceId != null\">AND a.departure_place_id = #{req.departurePlaceId}</if> \n" +
            "<if test=\"req.destinationId != null\">AND a.destination_id = #{req.destinationId}</if> \n" +
            "<if test=\"req.beginDate != null and req.endDate != null\">AND a.gmt_create between CONCAT(#{req.beginDate},' 00:00:00') and CONCAT(#{req.endDate},' 00:00:00') </if> \n" +
            "<if test=\"req.beginDate != null and req.endDate == null\">AND a.gmt_create between CONCAT(#{req.beginDate},' 00:00:00') and NOW()</if> \n" +
            "<if test=\"req.opId != null and req.opId != ''\">AND a.op_id = #{req.opId}</if> \n" +
            "<if test=\"req.bigClientId != null and req.bigClientId != 0\">AND a.big_client_id = #{req.bigClientId}</if> \n" +
            "<if test=\"req.list != null and !req.overTen\">AND a.travel_days = #{req.list}</if> \n" +
            "<if test=\"req.list == null and req.overTen\">AND a.travel_days > 10</if> \n" +
            "<if test=\"req.idList != null\"> OR b.visable_type = 3 AND a.id IN "+
                "<foreach collection=\"req.idList\" item=\"id1\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
                "#{id1}\n" +
                "</foreach>"+
            "</if> \n" +
            "<if test=\"req.permissionList != null\">AND a.create_id IN "+
                "<foreach collection=\"req.permissionList\" item=\"id2\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
                "#{id2}\n" +
                "</foreach>"+
            "</if> \n" +
            "ORDER BY a.gmt_create DESC"+"</script>")
    Page<CustomizedTripSchedule> customizedCenterList(@Param("req") TripScheduleCustomizedCenterListReq req, @Param("iPage") IPage<CustomizedTripSchedule> iPage);

    /**
     * 导领中心团期列表
     * @param idList
     * @return
     */
    @Select("<script>"+"SELECT a.id,a.group_status,a.travel_days,b.guide_requirement,b.need_trip_consumption,b.collection_place,c.order_people,c.total_people\n" +
            "FROM t_customized_trip_schedule a\n" +
            "LEFT JOIN t_customized_trip_schedule_advanced b ON a.id = b.schedule_id\n" +
            "LEFT JOIN t_customized_trip_schedule_amount_statistics c ON a.id = c.schedule_id\n" +
            "WHERE a.is_deleted = 0 AND a.id IN "+
            "<foreach collection=\"list\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
            "#{id}\n" +
            "</foreach>"+
            "</script>")
    List<TripScheduleGuideDTO> getScheduleList(@Param("list") List<Long> idList);

    /**
     * 根据团号获取团期ID
     * @param tripNumber
     * @return
     */
    @Select("SELECT id FROM t_customized_trip_schedule WHERE trip_number = #{trip_number} AND is_deleted = 0")
    Long queryIdByTripNumber(@Param("trip_number")String tripNumber);
}
